<template>
  <div class="itemAdd">
    <el-form :model="item" :rules="rules" ref="itemAdd" label-width="100px" class="demo-ruleForm">
			<el-form-item label="商品名称" prop="name">
			<el-input v-model="item.name"></el-input>
		</el-form-item>
		<el-form-item label="商品价格" prop="price">
			<el-input v-model="item.price"></el-input>
		</el-form-item>
		<el-form-item label="商品分类" prop="cateId">
			<el-select v-model="item.cateId" placeholder="请选择所属分类">
				<el-option
				v-for="cata in cates"
				:key="cata.id"
				:label="cata.cateName"
				:value="cata.id"></el-option>
			</el-select>
		</el-form-item>
		<el-form-item label="是否上架">
			 <el-switch
            v-model="item.onsale"
            :active-value="1"
            :inactive-value="0"></el-switch>
		</el-form-item>
		<el-form-item label="商品描述" prop="item.desc">
			<el-input type="textarea" v-model="item.desc"></el-input>
		</el-form-item>
		   <el-form-item label="分类图标">
          <el-upload
            class="upload-demo"
            name="file"
            action="/api/upload"
            :on-success="uploadSuccess"
            :on-error="uploadFail"
            :before-upload="beforeUpload"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
          <img :src="item.thumb" width="100" alt="" />
        </el-form-item>
		<el-form-item>
			<el-button type="primary" @click="submitForm()">立即增加</el-button>
			<el-button>取消</el-button>
		</el-form-item>
	</el-form>
  </div>
</template>

<script>
import { getCataLists } from '_api'
export default {
  data () {
    const validator1 = (rule, value, callback) => {
      if (value !== -1) {
        return callback()
      } else {
        return callback(new Error('必须选择一个分类'))
      }
    }
    return {
      item: {
        name: '',
        price: 0,
        cateId: -1,
        desc: '',
        thumb: '',
        onsale: 1,
        content: ''
      },
      cates: [],
      // 表单验证
      rules: {
        name: [
          {
            required: true,
            message: '此输入框必填'
          }
        ],
        price: [
          {
            type: 'number',
            required: true,
            message: '价格必须填写，而且要求为数字'
          }
        ],
        cateId: [
          {
            required: true,
            message: '此项必选'
          },
          {
            validator: validator1
          }
        ]
      }
    }
  },
  methods: {
		 onCreated (editor) {
      this.editor = Object.seal(editor)
    },
		 beforeUpload (file) {
      // 上传文件之前  限制上传文件的体积
      if (file.size > 1024 * 1024 * 5) {
        this.$message({
          type: 'error',
          message: '请上传小于5M体积的图标'
        })
        return false
      } else {
        return true
      }
    },
    uploadSuccess (res) {
      // 上传成功
      // 弹出成功提示
      this.$message({
        type: 'success',
        message: '图片上传成功'
      })
      this.item.thumb = res.data.url
    },
    uploadFail () {
      // 上传失败
    },
    getCates () {
      getCataLists().then((res) => {
        console.log(111, res)
        if (res.data.code === 200) {
          this.cates = res.data.data.list
        }
      })
    }
  },
  created () {
    this.getCates()
    console.log(this.cates, 222)
  }

}
</script>

<style>

</style>
